//
//  DoImagePickerCollectionViewCell.m
//  TBC_Harvest
//
//  Created by 李希朋 on 2017/4/7.
//  Copyright © 2017年 sdgh. All rights reserved.
//

#import "DoImagePickerCollectionViewCell.h"
@implementation DoImagePickerCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _selectBtn.layer.cornerRadius = _selectBtn.frame.size.width/2.0;
    _selectBtn.layer.masksToBounds = YES;
    _selectBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.selectBtn addTarget:self action:@selector(clickedSel:) forControlEvents:UIControlEventTouchUpInside];
    
//    self.selSubject = [RACSubject subject];
    
}

- (void)clickedSel:(id)sender
{
//    NSLog(@"tangbineee");
//    [self.selSubject sendNext:sender];
    if (self.selectBlock)
    {
        self.selectBlock(sender, self.libType);
    }
}

-(void)setAsset:(PHAsset*)asset
{
    if (asset.mediaType == PHAssetMediaTypeVideo)
    {
        PHVideoRequestOptions *voptions = [[PHVideoRequestOptions alloc] init];
        voptions.version = PHVideoRequestOptionsVersionOriginal;
        voptions.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
        [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:voptions resultHandler:^(AVAsset * _Nullable vasset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
            if ([vasset isKindOfClass:[AVURLAsset class]])
            {
//                NSLog(@"=====%@", [NSThread currentThread]);
                AVURLAsset *urlAsset = (AVURLAsset *)vasset;
//                NSNumber *size;
//                [urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
//                NSLog(@"size is %f",[size floatValue]/(1024.0*1024.0));
//                NSLog(@"info=%@", info);
                NSLog(@"视频路径=%@", urlAsset.URL.absoluteString);
//
                CMTime   time = [urlAsset duration];
                int seconds = ceil(time.value/time.timescale);
//                NSLog(@"seconds=%d", seconds);
                NSString *strTime = [EDSUtility getMMSSFromSS:seconds];
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.timeLabel.text = [strTime substringFromIndex:3];
                    self.timeLabel.hidden = NO;
                });
                
                // 获取视频原图
//                [[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
//                    // 得到原图
//                    UIImage *image = [UIImage imageWithData:imageData];
//                    NSLog(@"srcImage.size=%@; orientation=%ld", NSStringFromCGSize(image.size), (long)orientation);
//                }];
                
            }
        }];
    }
    
    
    
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.synchronous = NO;
//    options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
//    options.resizeMode = PHImageRequestOptionsResizeModeExact;
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    CGSize  size = CGSizeMake(300, 300);
    [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//        NSLog(@"===****==%@", [NSThread currentThread]);
        if (asset.mediaType == PHAssetMediaTypeVideo)
        {
//            NSLog(@"视频------");
            self.libType = 1;
//            self.timeLabel.hidden = NO;
        }
        else
        {
            self.timeLabel.hidden = YES;
            self.libType = 0;
        }
        self.photoImg.image = result;
    }];

}

-(void)clear
{
    _selectBtn.selected = NO;
    _selectBtn.hidden = NO;
    [_selectBtn setTitle:@"" forState:UIControlStateNormal];
}

@end
